Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIR borrowck: finalize check_access_permissions() #46041

Merged
merged 3 commits into from
Nov 30, 2017

Conversation

zilbuz
Copy link
Contributor

@zilbuz zilbuz commented Nov 16, 2017

Fix #44837 (hopefully for good)

r? @arielb1

@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 16, 2017
err.emit();
}
},
Write(WriteKind::MutableBorrow(BorrowKind::Shared)) |
Copy link
Contributor

@arielb1 arielb1 Nov 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a delay_span_bug if this would cause an error - all 3 of these cases indicate an erroneous program.

@@ -501,7 +513,9 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
lvalue_span: (&Lvalue<'tcx>, Span),
kind: ShallowOrDeep,
mode: MutateMode,
is_local_mutation_allowed: LocalMutationIsAllowed,
Copy link
Contributor

@arielb1 arielb1 Nov 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function checks for mutation of locals anyway, so it should always pass LocalMutationIsAllowed::Yes to avoid double errors.

let lvalue = lvalue_span.0;
let ty = lvalue.ty(self.mir, self.tcx).to_ty(self.tcx);
let moves_by_default =
self.fake_infer_ctxt.type_moves_by_default(self.param_env, ty, DUMMY_SP);
if moves_by_default {
// move of lvalue: check if this is move of already borrowed path
self.access_lvalue(context, lvalue_span, (Deep, Write(WriteKind::Move)), flow_state);
self.access_lvalue(context, lvalue_span, (Deep, Write(WriteKind::Move)), LocalMutationIsAllowed::No, flow_state);
Copy link
Contributor

@arielb1 arielb1 Nov 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is 100% OK to move out of an immutable local - this should be a yes.

@@ -549,7 +564,7 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
_ => unreachable!(),
};
self.access_lvalue(
context, (lvalue, span), (Shallow(Some(af)), Read(ReadKind::Copy)), flow_state);
context, (lvalue, span), (Shallow(Some(af)), Read(ReadKind::Copy)), LocalMutationIsAllowed::No, flow_state);
Copy link
Contributor

@arielb1 arielb1 Nov 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropandreplace is an assignment of a type with a destructor, so this should be an error only if the value is already initialized.

This is a read, ignore all of this.

@arielb1
Copy link
Contributor

arielb1 commented Nov 16, 2017

Commented. Need to leave for today.

@bors
Copy link
Contributor

bors commented Nov 16, 2017

☔ The latest upstream changes (presumably #45825) made this pull request unmergeable. Please resolve the merge conflicts.

@zilbuz
Copy link
Contributor Author

zilbuz commented Nov 17, 2017

@arielb1 Sorry I won't be available for the rest of the week and I didn't have the time to finalize my PR. I have some changes corresponding to your comments but not yet pushed. Some tests aren't passing.

If this is blocking feel free to take the PR and finalize it yourself. Otherwise I'll finish it sunday or monday evening.

@kennytm kennytm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 18, 2017
@@ -444,6 +446,12 @@ enum WriteKind {
Move,
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum LocalMutationIsAllowed {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can you add a comment explaining what this flag is about?

@zilbuz zilbuz force-pushed the issue-44837 branch 3 times, most recently from d3135c6 to d4cc8b0 Compare November 25, 2017 10:31
@zilbuz zilbuz changed the title [WIP] MIR borrowck: finalize check_access_permissions() MIR borrowck: finalize check_access_permissions() Nov 25, 2017
@zilbuz
Copy link
Contributor Author

zilbuz commented Nov 25, 2017

@nikomatsakis @arielb1 There is an ICE on the following test: https://github.com/rust-lang/rust/blob/master/src/test/run-pass/borrowck/borrowck-unsafe-static-mutable-borrows.rs

struct Foo { x: [usize; 2] }

static mut SFOO: Foo = Foo { x: [23, 32] };

impl Foo {
    fn x(&mut self) -> &mut usize { &mut self.x[0] }
}

fn main() {
    unsafe {
        let sfoo: *mut Foo = &mut SFOO;
        let x = (*sfoo).x();
        (*sfoo).x[1] += 1;
        *x += 1;
    }
}

The delay_span_bug() in check_access_permissions() is triggered for sfoo with Write(StorageDeadOrDrop): https://github.com/rust-lang/rust/pull/46041/files#diff-e91f32610bea593e68f8e93f87419a29R1048

How should I fix this? Is the Write(StorageDeadOrDrop) authorized in certain cases?

@arielb1
Copy link
Contributor

arielb1 commented Nov 25, 2017

@zilbuz

That's weird because the delay_span_bug should only occur for non-mut statics, and I don't see any non-mut static in this code.

Write(WriteKind::Move) |
Write(WriteKind::StorageDeadOrDrop) |
Write(WriteKind::MutableBorrow(BorrowKind::Shared)) => {
let item_msg = match self.describe_lvalue(lvalue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I think you're missing a check for whether the access is actually permitted, similarly to the previous lines. i.e. an
                if let Err(lvalue_err) = self.is_mutable(lvalue, is_local_mutation_allowed) { 
  1. There's no reason to use pretty-printing in delay_span_bug - it can hide information and it just makes the code uglier. Just format the lvalue directly (format!("Accessing {:?} with the kind {:?} shouldn't be possible", lvalue, kind)).

Some(name) => format!("`{}`", name),
None => "item".to_owned()
};
span_bug!(span, "&unique borrow for {} should not fail", item_msg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason to use pretty-printing in delay_span_bug - it can hide information and it just makes the code uglier. Just format the lvalue directly (format!("Accessing {:?} with the kind {:?} shouldn't be possible", lvalue, kind)).

@arielb1
Copy link
Contributor

arielb1 commented Nov 25, 2017

Oops wrong delay_span_bug. However, I think I found the problem.

@zilbuz zilbuz force-pushed the issue-44837 branch 2 times, most recently from 65b5a73 to 4bd53ae Compare November 26, 2017 20:15
@zilbuz
Copy link
Contributor Author

zilbuz commented Nov 26, 2017

@arielb1 I added LocalMutationIsAllowed::Yes for the access_lvalue() calls in visit_terminator_entry(): https://github.com/rust-lang/rust/pull/46041/files#diff-e91f32610bea593e68f8e93f87419a29R397

@bors
Copy link
Contributor

bors commented Nov 26, 2017

☔ The latest upstream changes (presumably #46106) made this pull request unmergeable. Please resolve the merge conflicts.

@arielb1
Copy link
Contributor

arielb1 commented Nov 27, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Nov 27, 2017

📌 Commit abd07b7 has been approved by arielb1

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 27, 2017
@bors
Copy link
Contributor

bors commented Nov 27, 2017

☔ The latest upstream changes (presumably #46022) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Nov 28, 2017

☔ The latest upstream changes (presumably #46312) made this pull request unmergeable. Please resolve the merge conflicts.

@arielb1
Copy link
Contributor

arielb1 commented Nov 29, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Nov 29, 2017

📌 Commit 1cd9d74 has been approved by arielb1

@bors
Copy link
Contributor

bors commented Nov 30, 2017

⌛ Testing commit 1cd9d74 with merge 909b94b...

bors added a commit that referenced this pull request Nov 30, 2017
MIR borrowck: finalize `check_access_permissions()`

Fix #44837 (hopefully for good)

r? @arielb1
@bors
Copy link
Contributor

bors commented Nov 30, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: arielb1
Pushing 909b94b to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants